﻿<?php //action 10?>
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
<?php //check if GET set
	if (!isset($_GET['userid'])) {
		$to = "client.php";
		redirect_to($to);
		exit;  
		};
 ?>

<script type="text/javascript" src="javascripts/jquery.js"></script>
<script type='text/javascript' src='javascripts/jquery.autocomplete.js'></script>
<link rel="stylesheet" type="text/css" href="stylesheets/jquery.autocomplete.css" />
<script type="text/javascript">
$().ready(function() {
	$("#pareigos").autocomplete("includes/get_pareigos.php", {
		width: 260,
		matchContains: true,
		//mustMatch: true,
		//minChars: 0,
		//multiple: true,
		//highlight: false,
		//multipleSeparator: ",",
		selectFirst: false
	});
});
</script> 
 
<?php //check if user has primary photo

	$query = "SELECT *";
	$query .= " FROM `foto`";
	$query .= " WHERE `foto`.userid =";
	$query .= "'".$_GET['userid']."'";
	$query .= " AND `pagrindine` = 1";
	//echo $query."</br>";
	$result_set = mysql_query($query, $connection);
	$subject = mysql_fetch_array($result_set);
	
	if (($subject['pagrindine']) !=0) {
	//$message = "Pagrindine foto jau yra";
	$pagridine_yra = 1;
	$foto_location = $subject['path'];
	$foto_id = $subject['fotoid'];
	
	} else {
	//$message = "Pagrindines foto nera";
	$pagridine_yra = 0;
	};
	
	
?>
<?php //check if user has bausme entered

	$query = "SELECT *";
	$query .= " FROM `bausme`";
	$query .= " WHERE `bausme`.userid =";
	$query .= "'".$_GET['userid']."'";
	//echo $query."</br>";
	$result_set = mysql_query($query, $connection);
	$bausme = mysql_fetch_array($result_set);
	
	if (($bausme['bausmeid']) !=0) {
	//$message = "Bausme jau yra";
	$bausme_yra = 1;
	// $foto_location = $subject['path'];
	// $foto_id = $subject['fotoid'];
	
	} else {
	//$message = "Bausmes nera";
	$bausme_yra = 0;
	};
	
	
?>

 <?php //procesinam ka gaunam is post

 
	if (isset($_POST['Submit'])) {
		if($_POST['pradzia'] ==NULL) {
			$message .= "</br> <strong>Looks like there's no start</strong>";
		}elseif($_POST['pabaiga'] ==NULL) {
			$message .= "</br> <strong>Looks like there's no end</strong>";
		}elseif($_POST['pradzia'] > $_POST['pabaiga']) {
			$message .= "</br> <strong>Looks like you make magic in time</strong>";
		}elseif(strtotime($_POST['pabaiga'])< (time(now))){
			$message .= "</br> <strong>Looks like your client has to be out</strong>";
		}elseif(($_POST['paskmet']*365+$_POST['paskmen']*30+$_POST['paskdien'])<($_POST['iskmet']*365+$_POST['iskmen']*30+$_POST['iskdien'])){
			$message .= "</br> <strong>Looks like your client got less than been</strong>";
		}elseif(($_POST['paskmet'] ==NULL) && ($_POST['paskmen'] ==NULL) && ($_POST['paskdien'] ==NULL)){
			$message .= "</br> <strong>Tai kas yra ta bausme?</strong>";
		}elseif (($_POST['paskmen'] >11) or ($_POST['iskmen'] >11)){
			$message .= "</br> <strong>a menesiu ne per daug?</strong>";
		}elseif (($_POST['paskdien'] >31) or ($_POST['iskdien'] >31)){
			$message .= "</br> <strong>a dienu ne per daug?</strong>";
		}else {
			if($bausme_yra ==1){
				$sql = "UPDATE `bausme` SET";
				$sql .= " `paskmet` =";
				if ($_POST['paskmet'] ==NULL){
				$sql .= 0;
				}else{
				$sql .= $_POST['paskmet'];
				};
				$sql .= " , `paskmen` =";
				if ($_POST['paskmen'] ==NULL){
				$sql .= 0;
				}else{
				$sql .= $_POST['paskmen'];
				};
				$sql .= " , `paskdien` =";
				if ($_POST['paskdien'] ==NULL){
				$sql .= 0;
				}else{
				$sql .= $_POST['paskdien'];
				};
				$sql .= " , `iskmet` =";
				if ($_POST['iskmet'] ==NULL){
				$sql .= 0;
				}else{
				$sql .= $_POST['iskmet'];
				};
				$sql .= " , `iskmen` =";
				if ($_POST['iskmen'] ==NULL){
				$sql .= 0;
				}else{
				$sql .= $_POST['iskmen'];
				};
				$sql .= " , `iskdien` =";
				if ($_POST['iskdien'] ==NULL){
				$sql .= 0;
				}else{
				$sql .= $_POST['iskdien'];
				};
				$sql .= " , `pradzia` =";
				$sql .= "'" . $_POST['pradzia'] . "' ";
				$sql .= " , `pabaiga` =";
				$sql .= "'" . $_POST['pabaiga'] . "' ";
				$sql .= " , `straipsnisid` =";
				$sql .= $_POST['straipsnisid'];
				$sql .= " WHERE `userid` = ";
				$sql .= ($_GET['userid']);
				$sql .= " LIMIT 1;";
				//echo $pre;
				$result = mysql_query($sql, $connection);
				if ($result) {
				
				//===========================================================//
				//   HZ kur redirect
				//===========================================================//
				
				
				$to = "summary.php?userid=" . $_GET['userid'];
				 redirect_to($to);
					// // Success!
					 //$message .= "</br>Client updated Successsullyy!! </br>";
				} else {
					// Display error message.
					$message .= "<p>Client update failed.</p>";
					$message .= "<p>" . mysql_error() . "</p>";
				};
			} else {
				$sql = "INSERT INTO `bausme` (`userid`, `paskmet`, `paskmen`, `paskdien`, `iskmet`, `iskmen`, `iskdien`, `pradzia`, `pabaiga`, `straipsnisid`) VALUES (";
				$sql .= $_GET['userid'];
				$sql .= " , ";
				if ($_POST['paskmet'] ==NULL){
				$sql .= 0;
				}else{
				$sql .= $_POST['paskmet'];
				};
				$sql .= " , ";
				if ($_POST['paskmen'] ==NULL){
				$sql .= 0;
				}else{
				$sql .= $_POST['paskmen'];
				};
				$sql .= " , ";
				if ($_POST['paskdien'] ==NULL){
				$sql .= 0;
				}else{
				$sql .= $_POST['paskdien'];
				};
				$sql .= " , ";
				if($_POST['iskmet'] == NULL){
				$sql .= 0;
				}else{
				$sql .= $_POST['iskmet'];
				};
				$sql .= " , ";
				if ($_POST['iskmen'] ==NULL){
				$sql .= 0;
				}else{
				$sql .= $_POST['iskmen'];
				};
				$sql .= " , ";
				if ($_POST['iskdien'] ==NULL){
				$sql .= 0;
				}else{
				$sql .= $_POST['iskdien'];
				};
				$sql .= " , ";
				$sql .= "'" . $_POST['pradzia'] . "' ";
				$sql .= " , ";
				$sql .= "'" . $_POST['pabaiga'] . "' ";
				$sql .= " , ";
				$sql .= $_POST['straipsnisid'];
				$sql .= ");";
				//$sql .= " LIMIT 1;";
				echo $sql;
				$result = mysql_query($sql, $connection);
				if ($result) {
				
				//===========================================================//
				//   HZ kur redirect
				//===========================================================//
				
				
				$to = "summary.php?userid=" . $_GET['userid'];
				 redirect_to($to);
					// // Success!
					 //$message .= "</br>Client bausme prideta Successsullyy!! </br>";
				} else {
					// Display error message.
					$message .= "<p>Client update failed.</p>";
					$message .= "<p>" . mysql_error() . "</p>";
				};		
			
			};
		};	
	};
	
 

 ?>


			
					<article>
						<h2>Srokas</h2>
	<?php echo $message."</br>";?>
	<?php //pagrindiniai kliento duomenys
	
	$query = "SELECT *";
	$query .= " FROM `user`";
	$query .= " WHERE `user`.userid =";
	$query .= "'".$_GET['userid']."'";
	
	//echo $query."</br>";
	$result_set = mysql_query($query, $connection);

	$subject = mysql_fetch_array($result_set);
	//print_r ($subject) ;
	
	//echo "Vardas: ";
	echo $subject['vardas'];
	echo ' ';
	
	// echo "Pavarde: ";
	echo $subject['pavarde'];
	echo ' ';	
	
	//echo "Bylos Nr.: ";
	echo $subject['bylosnr'];
	echo '</br>';
?>

    <?php //rodom pagrindine foto jeigu yra
	if ($pagridine_yra == 1){ 
		echo "</br> <img src=\"";
		echo $foto_location;
		echo "\" width=\"150\">";
	};

?> 
   </br></br>
<form autocomplete="off" action="bausme.php?userid=<?php echo ($_GET['userid']);?>" method="post" name="submit">
	<table width="330" border="0" cellspacing="2" cellpadding="3">
		
		<tr>
			<td>
			 Paskirta bausme: 
			</td>
			<td>
			  <input name="paskmet" type="text" id="paskmet" size="3" /></td>
			<td>met.</td>
			<td><input name="paskmen" type="text" id="paskmen" size="3" /></td>
			<td>men.</td>
			<td><input name="paskdien" type="text" id="paskdien" size="3" /></td>
			<td>d.</td>
		</tr>
		<tr>
		  <td>Iskaiciouta:</td>
		  <td><input name="iskmet" type="text" id="iskmet" size="3" /></td>
		  <td>met.</td>
		  <td><input name="iskmen" type="text" id="iskmen" size="3" /></td>
		  <td>men.</td>
		  <td><input name="iskdien" type="text" id="iskdien" size="3" /></td>
		  <td>d.</td>
		  </tr>
		<tr>
		  <td>Bausmes pradzia:</td>
		  <td colspan="5"><input name="pradzia" type="text" id="pabaiga" size="11" />
		    1900-12-31</td>
		  <td></td>
		  </tr>
		<tr>
		  <td>Bausmes pabaiga:</td>
		  <td colspan="5"><input name="pabaiga" type="text" id="pabaiga" size="11" />
		    1900-12-31</td>
		  <td></td>
		  </tr>
		<tr>
		  <td>Pagrindinis straipsnis:</td>
		  <td colspan="5"><label for="straipsnisid"></label>
		    <select name="straipsnisid" id="straipsnisid">
		      <option value="11">1</option>
		      <option value="22">2</option>
		      </select></td>
		  <td></td>
		  </tr>
		<tr>	  
			<td>

			
			<input name="Submit" type="submit" value="SubmiNt" />
			</td>
		</tr>
	</table>
</form>


			</article>
            
<?php require("includes/footer.php"); ?>
